package online.yysh.charging;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.google.gson.Gson;
import online.yysh.charging.base.BaseActivity;
import online.yysh.charging.bean.User;
import online.yysh.charging.bean.request.StopBookingRequest;
import online.yysh.charging.bean.response.GetAuthMsgResponse;
import online.yysh.charging.utils.HttpUtil;

import java.io.IOException;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Response;

public class MyPreChargeActivity extends BaseActivity implements View.OnClickListener {

    private TextView tv_cancel, tv_title, tv_stop;
    private TextView tv_time, tv_device_id, tv_port;
    private User user;

    //查询用户状态信息成功
    private static final int QUERY_USER_SUCCEED = 600;
    //查询用户状态信息失败
    private static final int QUERY_USER_FAILED = 601;

    //查询用户状态信息成功
    private static final int STOP_BOOKING_SUCCEED = 500;
    //查询用户状态信息失败
    private static final int STOP_BOOKING_FAILED = 501;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case QUERY_USER_SUCCEED:
                    if (8 == user.getStatus()) {
                        tv_time.setText("预约时间：" + user.getBookingTime());
                        tv_port.setText("枪号：" + user.getPort());
                        tv_device_id.setText("设备号：" + user.getDeviceId());
                    } else {
                        showToast("没有查询到预约记录");
                    }
                    break;
                case QUERY_USER_FAILED:
                    showToast("查询预约状态失败");
                    break;
                case STOP_BOOKING_FAILED:
                    showToast("取消预约失败");
                    break;
                case STOP_BOOKING_SUCCEED:
                    showToast("取消预约成功");
                    finish();
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.activity_my_pre_charge;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        tv_cancel = findViewById(R.id.tv_cancel);
        tv_cancel.setOnClickListener(this);
        tv_title = findViewById(R.id.tv_title);
        tv_title.setText(R.string.my_pre_charge);
        tv_stop = findViewById(R.id.tv_diy);
        tv_stop.setText(R.string.stop_pre_charge);
        tv_stop.setOnClickListener(this);
        tv_device_id = findViewById(R.id.tv_device_id);
        tv_port = findViewById(R.id.tv_port);
        tv_time = findViewById(R.id.tv_pre_time);
    }

    @Override
    protected void initData() {
        checkChargingStatus();
    }

    private void stopPreCharge() {
        if (user != null) {
            StopBookingRequest request = new StopBookingRequest(openid, user.getDeviceId(), user.getPort());
            HttpUtil.getInstance().stopBooking(request, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                    handler.sendEmptyMessage(STOP_BOOKING_FAILED);
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();
                    if (result.contains("ok")) {
                        handler.sendEmptyMessage(STOP_BOOKING_SUCCEED);
                    } else {
                        handler.sendEmptyMessage(STOP_BOOKING_FAILED);
                    }
                }
            });
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_diy:
                stopPreCharge();
                break;
            case R.id.tv_cancel:
                finish();
                break;
        }
    }

    /**
     * 检测当前账户充电状态
     */
    private void checkChargingStatus() {
        if (!TextUtils.isEmpty(openid)) {
            HttpUtil.getInstance().getAuthMsg(openid, new Callback() {
                @Override
                public void onFailure(Call call, IOException e) {
                    e.printStackTrace();
                }

                @Override
                public void onResponse(Call call, Response response) throws IOException {
                    String result = response.body().string();
                    Log.e(TAG, "getAuthMsg: " + result);
                    GetAuthMsgResponse data = new Gson().fromJson(result, GetAuthMsgResponse.class);
                    if ("ok".equals(data.getStatus())) {
                        user = data.getData();
                        handler.sendEmptyMessage(QUERY_USER_SUCCEED);
                    } else {
                        //获取信息失败
                        handler.sendEmptyMessage(QUERY_USER_FAILED);
                    }
                }
            });
        }
    }
}